package cn.chen.miaosha.controller;

import cn.chen.miaosha.domain.User;
import cn.chen.miaosha.redis.RedisService;
import cn.chen.miaosha.redis.UserKey;
import cn.chen.miaosha.result.CodeMsg;
import cn.chen.miaosha.result.Result;
import cn.chen.miaosha.service.MiaoshaUserService;
import cn.chen.miaosha.service.UserService;
import cn.chen.miaosha.util.ValidatorUtil;
import cn.chen.miaosha.vo.LoginVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;


@Controller
@RequestMapping("/login")
public class LoginController {

    private static Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private MiaoshaUserService miaoshaUserService;

    @RequestMapping("/to_login")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/do_login")
    @ResponseBody
    public Result<String> doLogin(HttpServletResponse response,@Valid LoginVO loginVO){
        logger.info(loginVO.toString());

//        String password = loginVO.getPassword();
//        String mobile = loginVO.getMobile();
//        if(StringUtils.isEmpty(password)){
//            return Result.error(CodeMsg.PASSWORD_EMPTY);
//        }
//        if(StringUtils.isEmpty(mobile)){
//            return Result.error(CodeMsg.MOBILE_EMPTY);
//        }
//        if(!ValidatorUtil.isMobile(mobile)){
//            return Result.error(CodeMsg.MOBILE_ERROR);
//        }
        String token = miaoshaUserService.login(response,loginVO);
        return Result.success(token);
    }

}
